import requests
import os
from multiprocessing import Process
import threading
import time
import sys
import cv2
import subprocess
directory = 'cache/input/'

mask_path = "cache/mask.png"
mask = open(mask_path, "rb").read()

url = "http://127.0.0.1:8080/inpaint"

def doit(imageName):
    #printProgressBar(0, end, prefix = 'cleaning extracted frames...', suffix = 'Complete', length = 50)
    img_path = directory + imageName;
    # print(img_path)
    image = open(img_path, "rb").read()

    response = requests.post(
        url,
        files={"image": image, "mask": mask},
        data={
            "ldmSteps": 25,
            "ldmSampler": "plms",
            "hdStrategy": "Original",
            "zitsWireframe": False,
            "hdStrategyCropMargin": 128,
            "hdStrategyCropTrigerSize": 512,
            "hdStrategyResizeLimit": 1280,
            "prompt": "",
            "negativePrompt": "",
            "useCroper": False,
            "croperX": 0,
            "croperY": 0,
            "croperHeight": 512,
            "croperWidth": 512,
            "sdScale": 1.0,
            "sdMaskBlur": 0,
            "sdStrength": 0.75,
            "sdSteps": 50,
            "sdGuidanceScale": 7.5,
            "sdSampler": "uni_pc",
            "sdSeed": 42,
            "sdMatchHistograms": False,
            "cv2Flag": "INPAINT_NS",
            "cv2Radius": 4,
            "paintByExampleSteps": 50,
            "paintByExampleGuidanceScale": 7.5,
            "paintByExampleMaskBlur": 0,
            "paintByExampleSeed": 42,
            "paintByExampleMatchHistograms": False,
            "paintByExampleExampleImage": None,
            "p2pSteps": 50,
            "p2pImageGuidanceScale": 7.5,
            "p2pGuidanceScale": 7.5,
            "controlnet_conditioning_scale": 0.4,
            "controlnet_method": "control_v11p_sd15_canny",
            "paint_by_example_example_image": None,
        },
    )

    with open(f"cache/output/{imageName}", "wb") as f:
        f.write(response.content)
    print("index=" + imageName)

if __name__ == "__main__":
    imageName = sys.argv[1] 
    doit(imageName)
    print("doneall")
    # th2.join()